package org.jeecg.modules.task.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.service.impl.JeecgServiceImpl;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.entity.ConActivityApplyItem;
import org.jeecg.modules.activity.entity.ConActivityApplyItemValue;
import org.jeecg.modules.activity.mapper.ConActivityApplyItemMapper;
import org.jeecg.modules.activity.mapper.ConActivityApplyItemValueMapper;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.content.entity.ConContentExtras;
import org.jeecg.modules.content.entity.ConContentVersion;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IConContentExtrasService;
import org.jeecg.modules.content.service.IConContentVersionService;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.interview.entity.InterviewUnion;
import org.jeecg.modules.interview.service.IInterviewUnionService;
import org.jeecg.modules.recommend.entity.RecommandInfo;
import org.jeecg.modules.recommend.service.IRecommandService;
import org.jeecg.modules.task.entity.TaskInfoBase;
import org.jeecg.modules.task.entity.TaskPublishState;
import org.jeecg.modules.task.mapper.TaskInfoBaseMapper;
import org.jeecg.modules.task.mapper.TaskPublishStateMapper;
import org.jeecg.modules.task.service.ITaskInfoBaseService;
import org.jeecg.modules.task.service.ITaskOperationService;
import org.jeecg.modules.title.entity.ConTitleRecord;
import org.jeecg.modules.title.service.IConTitleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 任务操作
 * @Author: wufanxin
 * @Date:  2021-07-01
 * @Version: V1.0
 */
@Service
public class TaskOperationServiceImpl implements ITaskOperationService {


    @Autowired
    private IContentInfoService contentInfoService;

    @Autowired
    private IActivityInfoService activityInfoService;


    @Autowired
    private ITaskInfoBaseService taskInfoBaseService;

    @Autowired
    private IInterviewUnionService interviewUnionService;

    @Autowired
    private IConContentExtrasService conContentExtrasService;

    @Autowired
    private IRecommandService recommandService;


    @Autowired
    IConContentVersionService versionService;


    @Autowired
    private ConActivityApplyItemMapper activityApplyItemMapper;
    @Autowired
    private ConActivityApplyItemValueMapper activityApplyItemValueMapper;
    @Autowired
    private TaskPublishStateMapper taskPublishStateMapper;

    @Autowired
    private IConTitleRecordService conTitleRecordService;
    /**
     * 更新文章内容信息
     * @param taskInfoBase
     * @param textContentInfo
     */
    public void updateContentInfo(TaskInfoBase taskInfoBase,Map<String,Object> textContentInfo){
        UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
        contentInfoUpdateWrapper.set("theme_id",textContentInfo.get("themeId"));
        contentInfoUpdateWrapper.set("theme_name",textContentInfo.get("themeName"));
        contentInfoUpdateWrapper.set("content_type",textContentInfo.get("contentType"));
        contentInfoUpdateWrapper.set("category_id",textContentInfo.get("categoryId"));
        contentInfoUpdateWrapper.set("category_name",textContentInfo.get("categoryName"));
        contentInfoUpdateWrapper.set("content_title",textContentInfo.get("contentTitle"));
        contentInfoUpdateWrapper.set("content_sub_title",textContentInfo.get("contentSubTitle"));
        contentInfoUpdateWrapper.set("content_introduce",textContentInfo.get("contentIntroduce"));
        contentInfoUpdateWrapper.set("mb_logo_url",textContentInfo.get("mbLogoUrl"));
        contentInfoUpdateWrapper.set("is_watermark",textContentInfo.get("isWatermark"));
        contentInfoUpdateWrapper.set("video_url",textContentInfo.get("videoUrl"));
        contentInfoUpdateWrapper.set("voice_url",textContentInfo.get("voiceUrl"));
        contentInfoUpdateWrapper.set("html_url",textContentInfo.get("htmlUrl"));

        contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
        contentInfoUpdateWrapper.eq("content_no",textContentInfo.get("contentNo"));

        try {
            QueryWrapper<ContentInfo> contentInfoQueryWrapper = new QueryWrapper<>();
            contentInfoQueryWrapper.eq("task_id", taskInfoBase.getTaskNo());
            contentInfoQueryWrapper.last(" limit 1");
            ContentInfo contentInfoOri = contentInfoService.getOne(contentInfoQueryWrapper);
            if(!contentInfoOri.getContentTitle().equals(textContentInfo.get("contentTitle")) || StringUtils.isEmpty(contentInfoOri.getContentTitle())){
                //记录标题修改记录
                if(!StringUtils.isEmpty(textContentInfo.get("contentTitle"))){
                    LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                    ConTitleRecord record = new ConTitleRecord();
                    record.setTitle(textContentInfo.get("contentTitle").toString());
                    record.setTaskNo(taskInfoBase.getTaskNo());
                    record.setContentNo(textContentInfo.get("contentNo").toString());
                    conTitleRecordService.save(record);
                }
            }
        }catch (Exception e){

        }
        contentInfoService.update(contentInfoUpdateWrapper);
    }

    /**
     * 更新活动内容信息
     * @param taskInfoBase
     * @param activityContentInfo
     */
    public void updateActivityInfo(TaskInfoBase taskInfoBase,Map<String,Object> activityContentInfo){
        UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
        activityInfoUpdateWrapper.set("theme_id",activityContentInfo.get("themeId"));
        activityInfoUpdateWrapper.set("theme_name",activityContentInfo.get("themeName"));
        activityInfoUpdateWrapper.set("category_id",activityContentInfo.get("categoryId"));
        activityInfoUpdateWrapper.set("category_name",activityContentInfo.get("categoryName"));
        activityInfoUpdateWrapper.set("activity_title",activityContentInfo.get("activityTitle"));
        activityInfoUpdateWrapper.set("activity_sub_title",activityContentInfo.get("activitySubTitle"));
        activityInfoUpdateWrapper.set("activity_introduction",activityContentInfo.get("activityIntroduction"));
        activityInfoUpdateWrapper.set("apply_limit_num",activityContentInfo.get("applyLimitNum"));
        activityInfoUpdateWrapper.set("apply_end_time",activityContentInfo.get("applyEndTime"));
        activityInfoUpdateWrapper.set("begin_time",activityContentInfo.get("beginTime"));
        activityInfoUpdateWrapper.set("end_time",activityContentInfo.get("endTime"));
        activityInfoUpdateWrapper.set("longitude",activityContentInfo.get("longitude"));
        activityInfoUpdateWrapper.set("latitude",activityContentInfo.get("latitude"));
        activityInfoUpdateWrapper.set("activity_addr",activityContentInfo.get("activityAddr"));
        activityInfoUpdateWrapper.set("activity_type",activityContentInfo.get("activityType"));
        activityInfoUpdateWrapper.set("img_url1",activityContentInfo.get("imgUrl1"));

        activityInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
//        activityInfoUpdateWrapper.eq("activity_no",activityContentInfo.get("activityNo"));
        activityInfoService.update(activityInfoUpdateWrapper);
    }


    /**
     * 根据taskno 查询任务信息
     * @param taskNo
     * @return
     */
    @Override
    public TaskInfoBase queryTaskInfoByTaskNo(String taskNo) {
        QueryWrapper<TaskInfoBase> taskInfoBaseQueryWrapper = new QueryWrapper<>();
        taskInfoBaseQueryWrapper.eq("task_no",taskNo).last(" limit 1");
        return taskInfoBaseService.getOne(taskInfoBaseQueryWrapper);
    }

    /**
     * 联合撰稿人信息
     * @param taskNo
     * @param interviewDraft
     */
    @Override
    public void dealInterviewUnion(String taskNo, Map<String,Object> interviewDraft) {
        QueryWrapper<InterviewUnion> unionQueryWrapper = new QueryWrapper<>();
        unionQueryWrapper.eq("task_no",taskNo);
        interviewUnionService.remove(unionQueryWrapper);
        //保存撰稿人
        List<Map<String,Object>> unionMapList = (List<Map<String,Object>>)interviewDraft.get("interviewUnion");
        List<InterviewUnion> unionList = new ArrayList<>();
        unionMapList.stream().forEach(item->{
            InterviewUnion union = new InterviewUnion();
            union.setTaskNo(taskNo);
            union.setUserId(item.get("userId").toString());
            union.setUserName(item.get("userName").toString());
            unionList.add(union);
        });
        interviewUnionService.saveBatch(unionList);
    }

    /**
     * 处理附加信息
     * @param taskNo
     * @param contentExtrasList
     */
    @Override
    public void dealContentAddition(String taskNo, List<Map<String, Object>> contentExtrasList) {
        QueryWrapper<ContentInfo> contentInfoQueryWrapper = new QueryWrapper<>();
        contentInfoQueryWrapper.eq("task_id",taskNo).last(" limit 1");
        ContentInfo contentInfo = contentInfoService.getOne(contentInfoQueryWrapper);
        List<ConContentExtras> contentExtrasObjList = new ArrayList<>();
        for(Map<String,Object> map : contentExtrasList) {
            ConContentExtras tempExtras = new ConContentExtras();
            tempExtras.setAttachContent(map.get("attachContent").toString());
            tempExtras.setAttachName(map.get("attachName").toString());
            tempExtras.setContentId(contentInfo.getContentNo());
            contentExtrasObjList.add(tempExtras);
        }
        //先清除
        QueryWrapper<ConContentExtras> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("content_id",contentInfo.getContentNo());
        conContentExtrasService.remove(deleteWrapper);
        conContentExtrasService.saveBatch(contentExtrasObjList);
    }


    @Override
    public void dealActivityAddition(String taskNo, List<Map<String,Object>> applyList) {
        QueryWrapper<ActivityInfo> activityInfoQueryWrapper = new QueryWrapper<>();
        activityInfoQueryWrapper.eq("task_id",taskNo).last(" limit 1");
        ActivityInfo activityInfo = activityInfoService.getOne(activityInfoQueryWrapper);

        // 删除原来的选项
        activityApplyItemMapper.delete(new QueryWrapper<ConActivityApplyItem>()
                .in("activity_id", activityInfo.getActivityNo()));

        // 删除原来的选项值
        activityApplyItemValueMapper.delete(new QueryWrapper<ConActivityApplyItemValue>()
                .in("activity_id", activityInfo.getActivityNo()));


        if(null != applyList && applyList.size() > 0) {
            applyList.stream().forEach(item->{
                ConActivityApplyItem applyItem = new ConActivityApplyItem();
//                List<ConActivityApplyItemValue> itemValueList = item.getItemValueList();
                List<Map<String,Object>> itemValueList = (List<Map<String,Object>>)item.get("itemValueList");
                applyItem.setActivityId(activityInfo.getActivityNo());
                applyItem.setItemName(item.get("itemName").toString());
                applyItem.setItemType(Integer.parseInt(item.get("itemType").toString()));
                applyItem.setId(UUIDGenerator.generate());

                activityApplyItemMapper.insert(applyItem);
                if(null!=itemValueList && itemValueList.size()>0) {
                    for(Map<String,Object> valueMap:itemValueList) {
                        ConActivityApplyItemValue itemValue = new ConActivityApplyItemValue();
                        itemValue.setItemId(applyItem.getId());
                        itemValue.setItemName(item.get("itemName").toString());
                        itemValue.setValueName(valueMap.get("valueName").toString());
                        itemValue.setActivityId(activityInfo.getActivityNo());
                        activityApplyItemValueMapper.insert(itemValue);
                    }
                }
            });

        }
    }

    /**
     * 处理相关推荐
     * @param taskInfoBase
     * @param recommand
     */
    @Override
    public void dealRecommand(TaskInfoBase taskInfoBase, List<String> recommand) {
        String id = getContentIdByTask(taskInfoBase);
        //保存相关推荐
        QueryWrapper<RecommandInfo> deleteRecommandWrapper = new QueryWrapper<>();
        deleteRecommandWrapper.eq("content_id",id);
        recommandService.remove(deleteRecommandWrapper);
        if(null != recommand && recommand.size() > 0){
            List<RecommandInfo> recommandInfoList = new ArrayList<>();
            for(String recommandId: recommand){
                RecommandInfo recommandInfo = new RecommandInfo();
                recommandInfo.setRecomContentId(recommandId);
                recommandInfo.setType(taskInfoBase.getTaskType());
                recommandInfo.setContentId(id);
                recommandInfoList.add(recommandInfo);
            }
            recommandService.saveBatch(recommandInfoList);
        }
    }


    /**
     * 根据任务获取那些附加
     * @param taskNo
     * @return
     */
    @Override
    public List<ConContentExtras> queryContentAdditionByTask(String taskNo) {
        //查询任务信息
        TaskInfoBase taskInfoBase = queryTaskInfoByTaskNo(taskNo);
        String contentId = getContentIdByTask(taskInfoBase);
        QueryWrapper<ConContentExtras> addtionWrapper = new QueryWrapper<>();
        addtionWrapper.eq("content_id", contentId);
//        List<ConContentExtras> list = conContentExtrasService.list(addtionWrapper);
        return conContentExtrasService.list(addtionWrapper);
    }

    /**
     * 根据任务获取相关推荐
     * @param taskNo
     * @return
     */
    @Override
    public List queryRecommandByTask(String taskNo) {
        //查询任务信息
        TaskInfoBase taskInfoBase = queryTaskInfoByTaskNo(taskNo);
        String id = getContentIdByTask(taskInfoBase);
        //查询推荐信息
        QueryWrapper<RecommandInfo> recommandInfoQueryWrapper  = new QueryWrapper<>();
        recommandInfoQueryWrapper.eq("content_id",id);
        List<RecommandInfo> recommandInfos = recommandService.list(recommandInfoQueryWrapper);
        List<String> ids = new ArrayList<>();
        recommandInfos.stream().forEach(item->{
            ids.add(item.getRecomContentId());
        });
        List finalList = new ArrayList();
        if(ids.size()>0){
            if(taskInfoBase.getTaskType().equals("0")) {
                QueryWrapper<ContentInfo> contentListQuery = new QueryWrapper<>();
                contentListQuery.in("content_no", ids);
                List<ContentInfo> contentList = contentInfoService.list(contentListQuery);
                finalList = contentList;
            }else{
                QueryWrapper<ActivityInfo> activityListQuery = new QueryWrapper<>();
                activityListQuery.in("activity_no", ids);
                List<ActivityInfo> contentList = activityInfoService.list(activityListQuery);
                finalList = contentList;
            }
        }
        return finalList;
    }

    /**
     * 保存任务所有信息
     * @param requestMap
     */
    @Override
    public void saveTaskInfo(Map<String, Object> requestMap) {

        //解析参数
        String taskNo = (String)requestMap.get("taskNo");
        //查询任务信息
        TaskInfoBase taskInfoBase = queryTaskInfoByTaskNo(taskNo);
        //处理采访初稿
        Map<String,Object> interviewDraft = (Map<String,Object>)requestMap.get("interviewDraft");
        if(null != interviewDraft ){
            //保存主内容
            saveContent(taskInfoBase,interviewDraft.get("draft").toString());
            //处理联合撰稿人信息
            dealInterviewUnion(taskNo,interviewDraft);
        }

        //富文本信息
        String richContent = (String)requestMap.get("richContent");
        if(!StringUtils.isEmpty(richContent)){
            //保存主内容
            saveContent(taskInfoBase,richContent);
        }

        //文本内容信息
        Map<String,Object> textContentInfo = (Map<String,Object>)requestMap.get("textContentInfo");
        if(null != textContentInfo){
            updateContentInfo(taskInfoBase,textContentInfo);
        }

        //文本内容附加信息
        List<Map<String,Object>> contentExtrasList = (List<Map<String,Object>>)requestMap.get("contentAddition");
        if(null != contentExtrasList && contentExtrasList.size() > 0) {
            dealContentAddition(taskNo,contentExtrasList);
        }

        //活动附加信息
        List<Map<String,Object>> applyList = (List<Map<String,Object>>)requestMap.get("activityAddition");
        if(null != applyList && applyList.size() > 0) {
            dealActivityAddition(taskNo,applyList);
        }

        //活动内容信息
        Map<String,Object> activityContentInfo = (Map<String,Object>)requestMap.get("activityContentInfo");
        if(null != activityContentInfo){
            updateActivityInfo(taskInfoBase,activityContentInfo);
        }
//
//        //保存发布信息
//        Map<String,Object> publishInfo = (Map<String,Object>)requestMap.get("publishInfo");
//        if(null != publishInfo){
//            savePublish(taskInfoBase,publishInfo);
//        }

        //处理相关推荐
        dealRecommand(taskInfoBase,(List<String>)requestMap.get("recommand"));

        //处理发布信息
        List<String> selectPublishChannel = (List<String>)requestMap.get("selectPublishChannel");
        if(null != selectPublishChannel && selectPublishChannel.size() > 0){
            //先删除原有信息
            QueryWrapper<TaskPublishState> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("task_no",taskNo);
            taskPublishStateMapper.delete(queryWrapper);
            //封装新信息
            for(String channel : selectPublishChannel){
                TaskPublishState taskPublishState = new TaskPublishState();
                taskPublishState.setId(UUIDGenerator.generate());
                taskPublishState.setChannel(channel);
                taskPublishState.setTaskNo(taskNo);
                taskPublishStateMapper.insert(taskPublishState);
            }
        }

        //处理发布信息
        List<Map<String,Object>> selectPublishChannelHandler = (List<Map<String,Object>>)requestMap.get("selectPublishChannelHandler");
        if(null != selectPublishChannelHandler && selectPublishChannelHandler.size() > 0){
            //先删除原有信息
            QueryWrapper<TaskPublishState> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("task_no",taskNo);
            taskPublishStateMapper.delete(queryWrapper);
            //封装新信息
            for(Map<String,Object> channel : selectPublishChannelHandler){
                TaskPublishState taskPublishState = new TaskPublishState();
                taskPublishState.setId(UUIDGenerator.generate());
                taskPublishState.setChannel(channel.get("value").toString());
                taskPublishState.setUserId(channel.get("operateId").toString());
                taskPublishState.setUserName(channel.get("operateName").toString());
                taskPublishState.setTaskNo(taskNo);
                taskPublishStateMapper.insert(taskPublishState);
            }
        }

    }


    /**
     * 保存任务所有信息
     * @param requestMap
     */
    @Override
    public String autoSaveTaskInfo(Map<String, Object> requestMap) {

        //解析参数
        String taskNo = (String)requestMap.get("taskNo");

        //查询任务信息
        TaskInfoBase taskInfoBase = queryTaskInfoByTaskNo(taskNo);

        String richContent = (String)requestMap.get("richContent");
        QueryWrapper<ConContentVersion> versionQueryWrapper1 = new QueryWrapper<>();
        versionQueryWrapper1.eq("task_no",taskNo).orderByDesc("create_time").last(" limit 1");
        ConContentVersion versionInfo1 = versionService.getOne(versionQueryWrapper1);
        if(!richContent.equals(versionInfo1.getContent())){
            return "当前不是最新版本，请刷新页面";
        }
        //判断版本

        //判断是否是活动
        Map<String,Object> activityContentInfo = (Map<String,Object>)requestMap.get("activityContentInfo");
        if(null != activityContentInfo && taskInfoBase.getTaskType().equals("0")) {
            taskInfoBase.setTaskType("1");
            taskInfoBaseService.updateById(taskInfoBase);
        }

        if(!StringUtils.isEmpty(requestMap.get("contentTitle"))){
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("content_title",requestMap.get("contentTitle"));
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
        }


        if(!StringUtils.isEmpty(requestMap.get("mbLogoUrl"))){
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("mb_logo_url",requestMap.get("mbLogoUrl"));
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
        }
//
//        //处理采访初稿
//        Map<String,Object> interviewDraft = (Map<String,Object>)requestMap.get("interviewDraft");
//        if(null != interviewDraft ){
//            //保存主内容
//            saveContent(taskInfoBase,interviewDraft.get("draft").toString());
//            //处理联合撰稿人信息
//            dealInterviewUnion(taskNo,interviewDraft);
//        }

        //富文本信息
        if(!StringUtils.isEmpty(richContent)){
            //保存主内容
            autoSaveContent(taskInfoBase,richContent);
        }

//        //文本内容信息
//        Map<String,Object> textContentInfo = (Map<String,Object>)requestMap.get("textContentInfo");
//        if(null != textContentInfo){
//            updateContentInfo(taskInfoBase,textContentInfo);
//        }
//
//        //文本内容附加信息
//        List<Map<String,Object>> contentExtrasList = (List<Map<String,Object>>)requestMap.get("contentAddition");
//        if(null != contentExtrasList && contentExtrasList.size() > 0) {
//            dealContentAddition(taskNo,contentExtrasList);
//        }



//        //活动内容信息
////        Map<String,Object> activityContentInfo = (Map<String,Object>)requestMap.get("activityContentInfo");
//        if(null != activityContentInfo){
//            updateActivityInfo(taskInfoBase,activityContentInfo);
//        }
//
//        //活动附加信息
//        List<Map<String,Object>> applyList = (List<Map<String,Object>>)requestMap.get("activityAddition");
//        if(null != applyList && applyList.size() > 0) {
//            dealActivityAddition(taskNo,applyList);
//        }
//
//        //保存发布信息
//        Map<String,Object> publishInfo = (Map<String,Object>)requestMap.get("publishInfo");
//        if(null != publishInfo){
//            savePublish(taskInfoBase,publishInfo);
//        }

        //处理相关推荐
//        dealRecommand(taskInfoBase,(List<String>)requestMap.get("recommand"));

        //违禁词检查
//        List<TblBannedWords> selfBannedWord = tblBannedWordsService.list();
//        AtomicReference<String> result = new AtomicReference<>("");
//
//        if(!StringUtils.isEmpty(richContent)){
//            selfBannedWord.stream().forEach(item->{
//                if(richContent.contains(item.getWord())){
//                    result.set("包含自定义违禁词" + item.getWord());
//                }
//            });
//            String resultStr = result.get();
//            if(resultStr.equals("")){
//                try {
//                    String resultJson = BannedWordUtil.textCensor(richContent);
//                    Map<String,Object> jsonObject = (Map<String,Object>)JSONObject.parse(resultJson);
//                    resultStr  = "" + ((List<Map<String,Object>>)jsonObject.get("data")).get(0).get("msg");
//                    return resultStr;
//                }catch (Exception e){
//
//                }
//            }else{
//                return resultStr;
//            }
//        }
        return "";

    }

    private String getContentIdByTask(TaskInfoBase taskInfoBase){
        String id = "";
        if(taskInfoBase.getTaskType().equals("0")){
            QueryWrapper<ContentInfo> contentInfoUpdateWrapper = new QueryWrapper<>();
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo()).last(" limit 1");
            id = contentInfoService.getOne(contentInfoUpdateWrapper).getContentNo();
        }else{
            QueryWrapper<ActivityInfo> activityInfoUpdateWrapper = new QueryWrapper<>();
            activityInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo()).last(" limit 1");
            id = activityInfoService.getOne(activityInfoUpdateWrapper).getActivityNo();
        }
        return id;
    }



    private void saveContent(TaskInfoBase taskInfoBase,String content){
//        if(taskInfoBase.getTaskType().equals("0")){
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("content",content);
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
//        }else{
//            UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
//            activityInfoUpdateWrapper.set("content",content);
//            activityInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
//            activityInfoService.update(activityInfoUpdateWrapper);
//        }
        // 保存版本
        ConContentVersion version = new ConContentVersion();
        version.setContent(content);
        version.setTask_no(taskInfoBase.getTaskNo());
        versionService.save(version);
    }


    private void autoSaveContent(TaskInfoBase taskInfoBase,String content) {
        if (taskInfoBase.getTaskType().equals("0")) {
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("content", content);
            contentInfoUpdateWrapper.eq("task_id", taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
        } else {
            UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
            activityInfoUpdateWrapper.set("content", content);
            activityInfoUpdateWrapper.eq("task_id", taskInfoBase.getTaskNo());
            activityInfoService.update(activityInfoUpdateWrapper);
        }
        // 保存版本
//        ConContentVersion version = new ConContentVersion();
//        version.setContent(content);
//        version.setTask_no(taskInfoBase.getTaskNo());
//        ve
    }

    @Override
    public void savePublish(TaskInfoBase taskInfoBase,Map<String,Object> publish){
        if(taskInfoBase.getTaskType().equals("0")){
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("release_mode", publish.get("releaseMode"));
            contentInfoUpdateWrapper.set("author_name", publish.get("authorName"));
            if(publish.get("releaseMode").equals("2")){
                contentInfoUpdateWrapper.set("timed_release_time", publish.get("releaseSetTime"));
            }else{
                contentInfoUpdateWrapper.set("release_time", DateUtils.now());

            }
            contentInfoUpdateWrapper.set("is_release", publish.get("releaseMode").equals("1")?"1":"2");
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
        }else{
            UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
            activityInfoUpdateWrapper.set("release_mode", publish.get("releaseMode"));
            if(publish.get("releaseMode").equals("2")){
                activityInfoUpdateWrapper.set("timed_release_time", publish.get("releaseSetTime"));
            }else{
                activityInfoUpdateWrapper.set("release_time", DateUtils.now());

            }
            activityInfoUpdateWrapper.set("is_release", publish.get("releaseMode").equals("1")?"1":"2");
            activityInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            activityInfoService.update(activityInfoUpdateWrapper);
        }
    }

    @Override
    public void updateTaskContentAndActivityRelease(String taskNo) {
        TaskInfoBase taskInfoBase = queryTaskInfoByTaskNo(taskNo);

        if(taskInfoBase.getTaskType().equals("0")){
            UpdateWrapper<ContentInfo> contentInfoUpdateWrapper = new UpdateWrapper<>();
            contentInfoUpdateWrapper.set("is_release","3");
            contentInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            contentInfoService.update(contentInfoUpdateWrapper);
        }else{
            UpdateWrapper<ActivityInfo> activityInfoUpdateWrapper = new UpdateWrapper<>();
            activityInfoUpdateWrapper.set("is_release", "3");
            activityInfoUpdateWrapper.eq("task_id",taskInfoBase.getTaskNo());
            activityInfoService.update(activityInfoUpdateWrapper);
        }
    }

}
